﻿using System;
using System.Collections.Generic;
using System.Text;
using JDML.FilterCondition;

namespace JDML.MappingClass
{
    public class DAOMapping
    {
        private List<FieldMapping> dbFields = new List<FieldMapping>();
        public Type ClassType { get; set; }
        public string ClassName { get; set; }
        public List<FieldMapping> DBFields
        {
            get
            {
                return dbFields;
            }
            set
            {
                dbFields = value;
                if (DBTableMapping != null)
                {
                    DBTableMapping.DBFields = value;
                }
                if (DBSQLMapping != null)
                {
                    DBSQLMapping.DBFields = value;
                }
            }
        }
        public TableMapping DBTableMapping { get; set; }
        public SQLMapping DBSQLMapping { get; set; }

        public DAOMapping()
        {
            DBTableMapping = new TableMapping();
            DBSQLMapping = new SQLMapping();
        }
        public bool Load(Object o)
        {
            if ((DBSQLMapping != null) && DBSQLMapping.SelectSQLCommands.ContainsKey(""))
            {
                return DBSQLMapping.Load(o);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.Load(o);
            }
            return false;
        }
        public bool Find(Object o)
        {
            /*
            if ((DBSQLMapping != null) && DBSQLMapping.SelectSQLCommands.ContainsKey(""))
            {
                return DBSQLMapping.Find(o);
            }
            else if (DBTableMapping!=null)
            {
                return DBTableMapping.Find(o);
            }
            return false;
             * */
            return Find(o, string.Empty);
        }
        public bool Find(Object o, string selectCommandName)
        {
            if ((DBSQLMapping != null) && DBSQLMapping.SelectSQLCommands.ContainsKey(selectCommandName))
            {
                return DBSQLMapping.Find(o, selectCommandName);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.Find(o);
            }
            return false;
        }
        public bool Find(Object o, Conditions conditions)
        {
            if (DBTableMapping != null)
            {
                return DBTableMapping.Find(o, conditions);
            }
            return false;
        }

        public IList<T> FindList<T>(Object o)
        {
            return FindList<T>(o, string.Empty);
        }

        public IList<T> FindList<T>(Object o, string selectCommandName)
        {
            if ((DBSQLMapping != null) && DBSQLMapping.SelectSQLCommands.ContainsKey(selectCommandName))
            {
                return DBSQLMapping.FindList<T>(o, selectCommandName);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.FindList<T>(o);
            }
            return null;
        }
        public IList<T> FindList<T>(Object o, Conditions conditions)
        {
            if (DBTableMapping != null)
            {
                return DBTableMapping.FindList<T>(o, conditions);
            }
            return null;
        }

        public bool Insert(Object o, string SQLName)
        {
            if ((DBSQLMapping != null) && DBSQLMapping.InsertSQLCommands.ContainsKey(SQLName))
            {
                return DBSQLMapping.Insert(o, SQLName);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.Insert(o);
            }
            else
            {
                return false;
            }
        }
        public bool Update(Object o, string SQLName = "")
        {
            if ((DBSQLMapping != null) && DBSQLMapping.UpdateSQLCommands.ContainsKey(""))
            {
                return DBSQLMapping.Update(o, SQLName);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.Update(o);
            }
            else
            {
                return false;
            }
        }
        public bool Delete(Object o, string SQLName = "")
        {
            if ((DBSQLMapping != null) && DBSQLMapping.DeleteSQLCommands.ContainsKey(""))
            {
                return DBSQLMapping.Delete(o, SQLName);
            }
            else if (DBTableMapping != null)
            {
                return DBTableMapping.Delete(o);
            }
            else
            {
                return false;
            }
        }
        public bool CreateTable(Object o)
        {
            if (DBTableMapping != null)
            {
                return DBTableMapping.CreateTable(o);
            }
            else
            {
                return false;
            }
        }
    }
}
